<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
    <meta name="viewport" content="width=device-width">
    <title>工厂模式try</title>
</head>
<body>
	<h1>工厂模式try</h1>
</body>
<script type="text/javascript">

    // var extend = (function () {
    //     var F = function () {
    //     };

    //     return function (Parent) {
    //         var Child;

    //         Child = function () {
    //             Child.superproto.constructor.apply(this, arguments);
    //         };

    //         F.prototype = Parent.prototype;
    //         Child.prototype = new F();
    //         Child.prototype.constructor = Child;
    //         Child.superproto = Parent.prototype;
            
    //         console.log('Child',Child.prototype)

    //         return Child;
    //     };
    // })();

    
    
    var Character = function(sex){ this.sex = sex},
        Warrior = function(sex){ Character.call(this,sex) },
        Mage = function(sex){ Character.call(this,sex) },
        Archer = function(sex){ Character.call(this,sex) },
        Player = function(){};

    Character.prototype={
        constructor:Character,
        level:function(){
            console.log('level')
            return this;
        },
        gather:function(){
            console.log('gather')
            return this;
        },
        fight:function(){
            console.log('fight')
            return this;
        },
        sayName:function(){
            var n = this._sayName();
            n.A();
            return this;
        },
        _sayName:function(){
            throw new Error("父类是抽象类不能直接调用，需要子类重写该方法");
        }
    }

    function extend(Child,Parent){

        var F = function(){};
        F.prototype = Parent.prototype;
        Child.prototype = new F();
        Child.prototype.constructor = Child;
        Child.superProto = Parent.prototype;

    }

    extend(Warrior,Character)
    extend(Mage,Character)
    extend(Archer,Character)

    Warrior.prototype._sayName=function(){
        var A = function(){
            console.log('Warrior:战士')
        }
        return {
            A:A
        }
    }
    
    Player.prototype.play = function(role,sex){
        var character;
        
        switch (role){
            case '战士':
                character = new Warrior(sex);
                break;
            case '法师':
                character = new Mage(sex);
                break;
            case '射手':
                character = new Archer(sex);
                break;
            default:
                character = new Warrior(sex);
        }
        console.log('character',character)
        // character.level();
        // character.gather();
        // character.fight();
        // character.sayName()
        return character;
    }

    var player = new Player();

    player.play('战士','男').sayName().level().gather().fight()
   
    player.play('法师').sayName()



</script>
</html>